/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.ufrgs.inf.prav.interop.metadata;

import br.ufrgs.inf.prav.interop.AppConfig;
import br.ufrgs.inf.prav.interop.util.PathParser;
import java.net.URL;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Fernando
 */
public class MetadataHandler {
    
    private static String configFileName = "config.xml";

    private String currentAppName = "";

    public MetadataHandler(){
    }

    private static Config getAppConfig(){
        AppConfig cfg = AppConfig.getInstance();
        return getAppConfig(FacesContext.getCurrentInstance());
    }

    //load the configuration file for the current application
    private static boolean loadAppConfig(FacesContext context){
        ExternalContext ectx = context.getExternalContext();
        String appName = getCurrentAppName(context);
        HttpServletRequest request = (HttpServletRequest)
                    ectx.getRequest();
        String reqURL = request.getRequestURL().toString();
        int indexOfPathInfo = reqURL.indexOf(appName);

        String configURL = reqURL.substring(0, indexOfPathInfo + appName.length());
        configURL += configFileName;

        System.out.println("ConfigURL: " + configURL);

        try {
            JDOMReader reader = new JDOMReader();
            Config config = reader.readXML(new URL(configURL));
            Map appAtts =  context.getExternalContext().getApplicationMap();
            appAtts.put(appName + "-config", config);
        } catch (Exception e) {
            System.out.println("Erro em loadAppConfig: " + e.getMessage());
        }

        return false;
    }

    //return the configuration for the current Application
    public static Config getAppConfig(FacesContext context){
        Map appAtts =  context.getExternalContext().getApplicationMap();
        try {
            Config config = (Config)appAtts.get(getCurrentAppName(context) + "-config");
            if (config != null)
                return config;
            else {
                if (loadAppConfig(context))
                    return (Config)appAtts.get(getCurrentAppName(context) + "-config");
                else
                    return null;
            }

        } catch (Exception e) {
            loadAppConfig(context);
        }
        return null;
    }

    //nnn
    public String getURLFromItemID(String itemID, String targetDevice){
        Config config = getAppConfig();
        List<MediaItem> items = config.getItems();
        String url = "";
        for (MediaItem item : items){
            if (item.getId().equals(itemID)){
                if (item.getTargetDeviceClass().equals(targetDevice))
                    url = item.getFile();
            }
        }

        //fix url to reflect the actual position of the page
        ExternalContext ectx = FacesContext.getCurrentInstance().getExternalContext();
        String appName = getCurrentAppName(FacesContext.getCurrentInstance());
        HttpServletRequest request = (HttpServletRequest)
                    ectx.getRequest();
        String reqURL = request.getRequestURL().toString();


        String mod = PathParser.getPathModifier(reqURL, appName);
        url = mod + url;
//        System.out.println("app: " + appName + " - reqURL: " + reqURL + " - mod: " + mod);

        return url;
    }

    //Get the name of the current Website
    private static String getCurrentAppName(FacesContext context){
        ExternalContext ectx = context.getExternalContext();
        String appName = ectx.getRequestPathInfo();
        appName = PathParser.setNumberOfDirs(appName, 2);
        return appName;
    }


    // <editor-fold defaultstate="collapsed" desc="Getters and Setters">
    // </editor-fold>

}
